package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.course.domain.CourseWaitSelection;
import com.ruoyi.course.service.ICourseWaitSelectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.CourseSelected;
import com.ruoyi.course.service.ICourseSelectedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 已选课程管理Controller
 *
 * @author ruoyi
 * @date 2023-10-22
 */
@RestController
@RequestMapping("/course/course_selected")
public class CourseSelectedController extends BaseController {
    @Autowired
    private ICourseSelectedService courseSelectedService;

    @Autowired
    private ICourseWaitSelectionService courseWaitSelectionService;

    /**
     * 查询已选课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseSelected courseSelected) {
        startPage();
        List<CourseSelected> list = courseSelectedService.selectCourseSelectedList(courseSelected);
        return getDataTable(list);
    }

    @Anonymous
    @GetMapping("/swlist")
    public AjaxResult swlist(CourseSelected courseSelected) {

        List<CourseSelected> list = courseSelectedService.selectCourseSelectedList(courseSelected);
        AjaxResult ajax = AjaxResult.success();
        return ajax.put("list", list);
    }


    /**
     * 学生选课
     */
    @Anonymous
    @PostMapping("/student_select")
    public AjaxResult student_select(@RequestBody CourseSelected courseSelected) {
        CourseWaitSelection courseWaitSelection = new CourseWaitSelection();
        CourseSelected courseSelectedCurrent = new CourseSelected();
        BeanUtils.copyProperties(courseSelected, courseSelectedCurrent);
        List<CourseSelected> courseSelectedList = courseSelectedService.selectCourseSelectedList(courseSelectedCurrent);

        if (courseSelectedCurrent.getSelectId() < 1 && courseSelectedCurrent.getStudentId() < 22201) {
            return error("选课号或学生号错误！");
        }

        if (!courseSelectedList.isEmpty()) {
            return error("你已选过该课程！");
        }

        if (!(courseSelectedService.insertCourseSelected(courseSelectedCurrent) > 0)) {
            return error("选课失败！");
        }

        System.out.println("11111111111111111111111111111111111111111111111111111111111");
        courseWaitSelection = courseWaitSelectionService.selectCourseWaitSelectionBySelectId(courseSelectedCurrent.getSelectId());
        courseWaitSelection.setSelectedNum(courseWaitSelection.getSelectedNum() + 1);
        courseWaitSelectionService.updateCourseWaitSelection(courseWaitSelection);

        return toAjax(1);
    }


    /**
     * 导出已选课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:export')")
    @Log(title = "已选课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseSelected courseSelected) {
        List<CourseSelected> list = courseSelectedService.selectCourseSelectedList(courseSelected);
        ExcelUtil<CourseSelected> util = new ExcelUtil<CourseSelected>(CourseSelected.class);
        util.exportExcel(response, list, "已选课程管理数据");
    }

    /**
     * 获取已选课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:query')")
    @GetMapping(value = "/{studentSelectId}")
    public AjaxResult getInfo(@PathVariable("studentSelectId") Long studentSelectId) {
        return success(courseSelectedService.selectCourseSelectedByStudentSelectId(studentSelectId));
    }

    /**
     * 新增已选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:add')")
    @Log(title = "已选课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseSelected courseSelected) {
        return toAjax(courseSelectedService.insertCourseSelected(courseSelected));
    }

    /**
     * 修改已选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:edit')")
    @Log(title = "已选课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseSelected courseSelected) {
        return toAjax(courseSelectedService.updateCourseSelected(courseSelected));
    }

    /**
     * 删除已选课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course_selected:remove')")
    @Log(title = "已选课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentSelectIds}")
    public AjaxResult remove(@PathVariable Long[] studentSelectIds) {
        return toAjax(courseSelectedService.deleteCourseSelectedByStudentSelectIds(studentSelectIds));
    }
}
